package org.mybatis.example.batch.insert;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.example.BlogMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouchaoqun
 * @date 2018/11/21 下午1:45
 */
public class MybatisDynSqlBatchTest {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            long start = System.currentTimeMillis();

            List<String> list = new ArrayList<>(2);
            for (int i = 0; i < 10000; i++) {
                list.add("name" + i);
            }
            sqlSession.getMapper(BlogMapper.class).insertBatch(list);

            sqlSession.commit();

            System.out.println("use time : " + (System.currentTimeMillis() - start) + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
